package com.graduate.twentyone.controller;


import com.graduate.twentyone.domain.RestDate;
import com.graduate.twentyone.domain.Sale;
import com.graduate.twentyone.service.SaleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@CrossOrigin
public class SaleController {
    @Autowired
    SaleService saleService;


    @RequestMapping(value = "/sale", method = RequestMethod.GET)
    @RequiresPermissions("sale:select")
    public RestDate selectsale(Sale sale) {
        System.out.println("sale select");

        List<Sale> selectsale = saleService.selectsale(sale);
        RestDate restDate = new RestDate(500, "查询失败", selectsale);
//        for (Sale sale1 : selectsale) {
//            System.out.println("sale1 = " + sale1);
//        }
        if (selectsale.size() >= 0) {
            restDate = new RestDate(200, "查询成功", selectsale);
        }
        return restDate;
    }

    @PostMapping("/findsale")
    @RequiresPermissions("sale:select")
    public RestDate find(@RequestBody Sale sale) {
        System.out.println("sale find");
        System.out.println(sale.getDatetime() + "    datetime");
        System.out.println(sale.getUid() + "    uid");
        List<Sale> selectsale = saleService.findsale(sale);
        RestDate restDate = new RestDate(500, "查询失败", selectsale);
        if (selectsale.size() >= 0) {
            restDate = new RestDate(200, "查询成功", selectsale);
        }
        return restDate;
    }


    @RequestMapping(value = "/sale", method = RequestMethod.POST)
    @RequiresPermissions("sale:add")
    public RestDate addsale(@RequestBody List<Sale> saleList) {
        System.out.println("sale add");

        SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String format = tempDate.format(date);
        System.out.println(date + "    data");
        System.out.println(format + "    format");

        RestDate restDate = new RestDate(200, "添加成功", format);
        for (Sale sale : saleList) {
            sale.setDatetime(date);
            Integer addsale = saleService.addsale(sale);
            if (addsale != 1) {
                restDate = new RestDate(500, "添加失败", 00000);
                return restDate;
            }
        }
        return restDate;
    }


    @RequestMapping(value = "/sale", method = RequestMethod.PUT)
    @RequiresPermissions("sale:update")
    public RestDate updatesale(@RequestBody Sale sale) {
        System.out.println("sale update");
        Integer updatesale = saleService.updatesale(sale);
        System.out.println("updatesale = " + updatesale);
        RestDate restDate = new RestDate(500, "修改失败", 00000);
        if (updatesale != 0) {
            restDate = new RestDate(200, "修改成功", 00000);
        } else {
            restDate = new RestDate(500, "修改失败", 00000);
        }
        return restDate;
    }


    @RequestMapping(value = "/sale", method = RequestMethod.DELETE)
    @RequiresPermissions("sale:delete")
    public RestDate deletesale(@RequestBody List<Sale> saleList) {
        System.out.println("sale delete");

        RestDate restDate = new RestDate(200, "删除成功", 00000);
        for (Sale sale : saleList) {
            sale.setStatus(0);
            Integer updatesale = saleService.updatesale(sale);
            if (updatesale != 1) {
                restDate = new RestDate(500, "删除失败", 00000);
                return restDate;
            }
        }
        return restDate;
    }


}
